﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;
using Utility;
using System.Configuration;

public partial class Admin_ChiTietDonHang : System.Web.UI.Page
{
    protected string CurrentColumnSort
    {
        get
        {
            return ViewState["CurrentColumnSort"].ToString();
        }
        set
        {
            ViewState["CurrentColumnSort"] = value;
        }
    }
    protected int STT
    {
        get
        {
            return int.Parse(ViewState["STT"].ToString());
        }
        set
        {
            ViewState["STT"] = value;
        }
    }
    protected string OrderType
    {
        get
        {
            return ViewState["OrderType"].ToString();
        }
        set
        {
            ViewState["OrderType"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {
        //try
        //{
        STT = (pager.CurrentIndex - 1) * pager.PageSize + 1;
            int id = int.Parse(Request.QueryString["id"].ToString());
            HoaDonBanHang DonHang = HoaDonBanHangBUS.GetById(id);
            lit_MaDH.Text = DonHang.MaHoaDonBanHang.ToString();
            lit_NhanVien.Text = DonHang.TenDangNhap_NhanVien.ToString();

            NguoiMua KhachHang = NguoiMuaBUS.GetById(DonHang.TenDangNhap_NguoiMua);
            lit_KhachHang.Text = KhachHang.TenNguoiMua;
            lit_NgayDatHang.Text = DonHang.NgayDat.ToString();
            lit_NgayGiaoHang.Text = DonHang.NgayGiao.ToString();
            lit_TriGia.Text = String.Format("{0:0,0 VNĐ}", DonHang.ThanhTien);

            int trangthai = DonHang.MaTinhTrangHoaDon;
            lit_TrangThai.Text = trangthai.ToString();

            switch (trangthai)
            {
                case 0:
                    {
                        lit_TrangThai.Text = "None";
                        break;
                    }
                case 1:
                    {
                        lit_TrangThai.Text = "Vừa giao hàng";
                        break;
                    }
                case 2:
                    {
                        lit_TrangThai.Text = "Đã thanh toán";
                        break;
                    }
                case 3:
                    {
                        lit_TrangThai.Text = "Đã giao hàng";
                        break;
                    }
                case 4:
                    {
                        lit_TrangThai.Text = "Hoàn thành";
                        break;
                    }
            }

            lit_DiaChiGiaoHang.Text = DonHang.DiaChiGiaoHang;

            int total = 0;
            pager.PageSize = 1;
            Criteria cri = new Criteria();

            cri.add(Condition.AND, ChiTietHoaDonBanHangColumn.MaHoaDonBanHang,
                Condition.LIKE, DonHang.MaHoaDonBanHang);

            ChiTietHoaDonBanHangCollection CTDHCollection =
                ChiTietHoaDonBanHangBUS.GET(cri.Criter, pager.CurrentIndex, pager.PageSize,
                ChiTietHoaDonBanHangColumn.MaHoaDonBanHang.ToString(),
                "ASC", out total);
            pager.ItemCount = total;
            rpt_DonHang.DataSource = CTDHCollection;
            rpt_DonHang.DataBind();

            if (pager.ItemCount <= 0 || pager.ItemCount <= pager.PageSize)
                pager.Visible = false;
            else
                pager.Visible = true;
        //}
        //catch
        //{
        //    Response.Redirect("DonHang.aspx");
        //}
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }

    protected void rpt_DonHang_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            ChiTietHoaDonBanHang CTDH = (ChiTietHoaDonBanHang)e.Item.DataItem;
            SanPham sanpham = SanPhamBUS.GetById(CTDH.MaSanPham);
            
            if (sanpham != null)
            {
                Label lb_STT = (Label)e.Item.FindControl("lb_STT");
                lb_STT.Text = Convert.ToString(STT + e.Item.ItemIndex);

                Literal lit_TenSanPham = (Literal)e.Item.FindControl("lit_TenSanPham");
                lit_TenSanPham.Text = sanpham.TenSanPham;

                Literal lit_DonGia = (Literal)e.Item.FindControl("lit_DonGia");
                lit_DonGia.Text = String.Format("{0:0,0 VNĐ}", sanpham.Gia);

                Literal thanhtien = (Literal)e.Item.FindControl("lit_thanhtien");
                int tongtien = CTDH.SoLuong * CTDH.Gia;
                thanhtien.Text = String.Format("{0:0,0 VNĐ}", tongtien);
            }


        }
    }
}
